package com.onlineMIS.ORM.entity.headQ.inventoryFlow;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.ExcelTemplate;

public class HeadqInventoryChangeReportTemplate extends ExcelTemplate{
	private List<HqInventoryChangeDetailsVO> items = new ArrayList<HqInventoryChangeDetailsVO>();
	private HeadQInventoryStore store;
	private String storeName;
	
	private int data_row = 3;

	

    public String getStoreName() {
		return storeName;
	}

	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}

	public HeadqInventoryChangeReportTemplate(File file) throws IOException{
    	super(file);
    }
	
	public HeadqInventoryChangeReportTemplate(List<HqInventoryChangeDetailsVO> items, String storeName, String templateWorkbookPath) throws IOException{
		super(templateWorkbookPath);	
		this.items = items;
		this.storeName = storeName;

	}
	
	public HSSFWorkbook process(){
		HSSFSheet sheet = templateWorkbook.getSheetAt(0);
		if (StringUtils.isNotBlank(storeName)){
			Row header1 = sheet.getRow(0);
			header1.createCell(1).setCellValue(storeName);
		}
		
		Row header2 = sheet.getRow(1);
		header2.createCell(1).setCellValue(Common_util.dateFormat_f.format(new Date()));
		
		int totalDataRow = items.size();
		int totalQuantity = 0;
		double totalCost = 0;
		double totalSale = 0;
		double totalWholeSales = 0;
		for (int i = 0; i < totalDataRow; i++){

			HqInventoryChangeDetailsVO levelFourItem = items.get(i);
			Row row = sheet.createRow(data_row + i);

			row.createCell(0).setCellValue(levelFourItem.getName());
			
			row.createCell(1).setCellValue(levelFourItem.getBeforeAmount());
			
			row.createCell(2).setCellValue(levelFourItem.getBeforeAmountMoney());
			
			row.createCell(3).setCellValue(levelFourItem.getPeriodPurchaseNums());
			
			row.createCell(4).setCellValue(levelFourItem.getPeriodPurchaseMoneys());

			row.createCell(5).setCellValue(levelFourItem.getPeriodSaleReturnNums());
			
			row.createCell(6).setCellValue(levelFourItem.getPeriodSaleReturnMoneys());
			
			row.createCell(7).setCellValue(levelFourItem.getPeriodOverflowNums());
			
			row.createCell(8).setCellValue(levelFourItem.getPeriodOverflowMoneys());
			
			row.createCell(9).setCellValue(levelFourItem.getPeriodReceiveNums());

			row.createCell(10).setCellValue(levelFourItem.getPeriodReceiveMoneys());
			
			row.createCell(11).setCellValue(levelFourItem.getPeriodCallInNums());
			
			row.createCell(12).setCellValue(levelFourItem.getPeriodCallInMoneys());
			
			row.createCell(13).setCellValue(levelFourItem.getPeriodEnterNums());
			
			row.createCell(14).setCellValue(levelFourItem.getPeriodEnterMoneys());
			
			row.createCell(15).setCellValue(levelFourItem.getPeriodSaleNums());
			
			row.createCell(16).setCellValue(levelFourItem.getPeriodSaleMoneys());
			
			row.createCell(17).setCellValue(levelFourItem.getPeriodPurchaseReturnNums());
			
			row.createCell(18).setCellValue(levelFourItem.getPeriodPurchaseReturnMoneys());
			
			row.createCell(19).setCellValue(levelFourItem.getPeriodLossNums());
			
			row.createCell(20).setCellValue(levelFourItem.getPeriodLossMoneys());
			
			row.createCell(21).setCellValue(levelFourItem.getPeriodGiftNums());
			
			row.createCell(22).setCellValue(levelFourItem.getPeriodGiftMoneys());
			
			row.createCell(23).setCellValue(levelFourItem.getPeriodOutNums());
			
			row.createCell(24).setCellValue(levelFourItem.getPeriodOutMoneys());
			
			row.createCell(25).setCellValue(levelFourItem.getPeriodOutBoundNums());
			
			row.createCell(26).setCellValue(levelFourItem.getPeriodOutBoundMoneys());
			
			row.createCell(27).setCellValue(levelFourItem.getTotalQuantity());
			
			row.createCell(28).setCellValue(levelFourItem.getTotalAmount());
						
		}
		
		Row row = sheet.createRow(totalDataRow + 3);
		row.createCell(0).setCellValue("合计");		
		return templateWorkbook;
	}
	
	
	public HSSFWorkbook processDetails(){
		HSSFSheet sheet = templateWorkbook.getSheetAt(0);
		if (StringUtils.isNotBlank(storeName)){
			Row header1 = sheet.getRow(0);
			header1.createCell(1).setCellValue(storeName);
		}
		
		Row header2 = sheet.getRow(1);
		header2.createCell(1).setCellValue(Common_util.dateFormat_f.format(new Date()));
		
		int totalDataRow = items.size();
		int totalQuantity = 0;
		double totalCost = 0;
		double totalSale = 0;
		double totalWholeSales = 0;
		for (int i = 0; i < totalDataRow; i++){

			HqInventoryChangeDetailsVO levelFourItem = items.get(i);
			ProductBarcode pb = levelFourItem.getPb();
			Row row = sheet.createRow(data_row + i);

			row.createCell(0).setCellValue(pb.getProduct().getYear().getYear());
			
			row.createCell(1).setCellValue(pb.getProduct().getQuarter().getQuarter_Name());
			
			row.createCell(2).setCellValue(pb.getProduct().getBrand().getBrand_Name());
			
			row.createCell(3).setCellValue(pb.getProduct().getCategoryLevel1().getCategory_Name());
			
			row.createCell(4).setCellValue(pb.getProduct().getCategory().getCategory_Name());
			
			row.createCell(5).setCellValue(levelFourItem.getBeforeAmount());
			
			row.createCell(6).setCellValue(levelFourItem.getBeforeAmountMoney());
			
			row.createCell(7).setCellValue(levelFourItem.getPeriodPurchaseNums());
			
			row.createCell(8).setCellValue(levelFourItem.getPeriodPurchaseMoneys());

			row.createCell(9).setCellValue(levelFourItem.getPeriodSaleReturnNums());
			
			row.createCell(10).setCellValue(levelFourItem.getPeriodSaleReturnMoneys());
			
			row.createCell(11).setCellValue(levelFourItem.getPeriodOverflowNums());
			
			row.createCell(12).setCellValue(levelFourItem.getPeriodOverflowMoneys());
			
			row.createCell(13).setCellValue(levelFourItem.getPeriodReceiveNums());

			row.createCell(14).setCellValue(levelFourItem.getPeriodReceiveMoneys());
			
			row.createCell(15).setCellValue(levelFourItem.getPeriodCallInNums());
			
			row.createCell(16).setCellValue(levelFourItem.getPeriodCallInMoneys());
			
			row.createCell(17).setCellValue(levelFourItem.getPeriodEnterNums());
			
			row.createCell(18).setCellValue(levelFourItem.getPeriodEnterMoneys());
			
			row.createCell(19).setCellValue(levelFourItem.getPeriodSaleNums());
			
			row.createCell(20).setCellValue(levelFourItem.getPeriodSaleMoneys());
			
			row.createCell(21).setCellValue(levelFourItem.getPeriodPurchaseReturnNums());
			
			row.createCell(22).setCellValue(levelFourItem.getPeriodPurchaseReturnMoneys());
			
			row.createCell(23).setCellValue(levelFourItem.getPeriodLossNums());
			
			row.createCell(24).setCellValue(levelFourItem.getPeriodLossMoneys());
			
			row.createCell(25).setCellValue(levelFourItem.getPeriodGiftNums());
			
			row.createCell(26).setCellValue(levelFourItem.getPeriodGiftMoneys());
			
			row.createCell(27).setCellValue(levelFourItem.getPeriodOutNums());
			
			row.createCell(28).setCellValue(levelFourItem.getPeriodOutMoneys());
			
			row.createCell(29).setCellValue(levelFourItem.getPeriodOutBoundNums());
			
			row.createCell(30).setCellValue(levelFourItem.getPeriodOutBoundMoneys());
			
			row.createCell(31).setCellValue(levelFourItem.getTotalQuantity());
			
			row.createCell(32).setCellValue(levelFourItem.getTotalAmount());
						
		}
		
		Row row = sheet.createRow(totalDataRow + 3);
		row.createCell(0).setCellValue("合计");		
		return templateWorkbook;
	}
}
